<?php

namespace app\adminapi\controller\v2;

use app\adminapi\controller\AuthController;
use app\model\admin\TrainTypeModel;
use app\model\plan\Plan as PlanPlan;
use app\model\plan\PlanStaff;
use app\model\staff\Staff;
use think\Request;

class Plan extends AuthController
{
    public function list(Request $request)
    {
        $page = $request->param('page',1);
        $limit = $request->param('size', 10);

        $code = $request->param('code', '');
        $plan_name = $request->param('plan_name', '');
        $time_status = $request->param('time_status', '');
        $date = $request->param('date', '');
        $type_id = $request->param('type_id', '');

        $data = \app\model\plan\Plan::order('id','desc')
            ->when($code,function($query) use ($code){
                $query->where('code', 'like', '%'.$code.'%');
            })
            ->when($plan_name,function($query) use ($plan_name){
                $query->where('plan_name', 'like', '%'.$plan_name.'%');
            })
            ->when($time_status !== '',function($query) use ($time_status){
                $query->where('time_status', $time_status);
            })
            ->when($date, function($query) use ($date){
                $start = strtotime(date('Y-m-d 00:00:00', strtotime($date[0])));
                $end = strtotime(date('Y-m-d 23:59:59', strtotime($date[1])));
                $query->whereBetween('start_time', [$start, $end]);
            })
            ->when($type_id,function($query) use ($type_id){
                $query->where('type_id', 'like', '%,'.$type_id.',%');
            })
            ->where('status', 1)
            ->paginate($limit)
            ->each(function ($item) {
                $typeIds = explode(',', $item['type_ids']);
                $types = TrainTypeModel::whereIn('id', $typeIds)
                    ->column('title');
                $item['type_name'] = implode(',', $types);
                $item['start_time'] = date('Y-m-d', $item['start_time']);
                $item['end_time'] = date('Y-m-d', $item['end_time']);
            });

        return app('json')->success([
            'total' => $data->total(),
            'list' => $data->items(),
        ]);
    }

    public function info(Request $request)
    {
        return app('json')->success([
            'type_list' => DataType::getTrainType(),
        ]);
    }

    public function staffList(Request $request)
    {
        $plan_id = input('plan_id', '');
        $page = input('page', 1);
        $limit = input('size', 10);

        $where = [];
        $username = input('username', '');
        if ($username) {
            $staff_ids = Staff::where('username', 'like', '%'.$username.'%')
                ->column('id');
            $where[] = ['staff_id', 'in', $staff_ids];
        }
        $idcard = input('idcard', '');
        if ($idcard) {
            $staff_ids = Staff::where('idcard', 'like', '%'.$idcard.'%')
                ->column('id');
            $where[] = ['staff_id', 'in', $staff_ids];
        }
        $mobile = input('mobile', '');
        if ($mobile) {
            $staff_ids = Staff::where('mobile', 'like', '%'.$mobile.'%')
                ->column('id');
            $where[] = ['staff_id', 'in', $staff_ids];
        }
        $study_status = input('study_status', '');

        $staffs = PlanStaff::where('plan_id', $plan_id)
            ->with('staff')
            ->where($where)
            ->when($study_status,function($query) use ($study_status) {
                $query->where('study_status', $study_status);
            })
            ->paginate($limit)
            ->each(function ($item) {
                $item['staff']['idcard'] = substr_replace($item['staff']['idcard'], '****', 6, 8);
            });

        return app('json')->success([
            'list' => $staffs->items(),
            'total' => $staffs->total(),
        ]);
    }
}